package com.piece.blog.service;

import com.piece.blog.model.ArticleTag;
import com.piece.blog.repository.ArticleTagRepository;
import com.piece.core.framework.constant.FrameWorkConstants;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.jpa.repository.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArticleTagService extends BaseService<ArticleTag, String> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ArticleTagRepository articleTagRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(articleTagRepository);
    }

    public void batchUpdate(String names, long articleId, Long createBy) {
        if (StringUtil.isBlank(names.trim())) {
            return;
        }

        String[] ns = names.split(FrameWorkConstants.SPLIT_PARAM);
        Date current = new Date();
        String sql_article_tag_count = "select count(1) from blog_article_tag_rela where article_id=? and tag_id=?";
        String sql_article_tag_rela = "insert into blog_article_tag_rela(article_id, tag_id) values(?, ?)";
        for (String n : ns) {
            String name = n.trim();
            if (StringUtil.isBlank(name)) {
                continue;
            }

            Map<String, Object> condition = new HashMap<>();
            condition.put("name", name);
            ArticleTag tag = articleTagRepository.findOneByCondition(condition);
            if (null != tag) {
                int count = jdbcTemplate.queryForObject(sql_article_tag_count, Integer.class, new Object[]{articleId, tag.getTagId()});
                if (count > 0) {
                    continue;
                }
                tag.setPosts(tag.getPosts() + 1);
                articleTagRepository.update(tag);
            } else {
                tag = new ArticleTag();
                tag.setName(name);
                tag.setCreateBy(createBy);
                tag.setCreateTime(current);
                tag.setPosts(1);
                articleTagRepository.insert(tag);
                jdbcTemplate.update(sql_article_tag_rela, new Object[]{articleId, tag.getTagId()});
            }
        }
    }

    public void batchDelete(long articleId) {
        String sql_article_tag_id = "select tag_id from blog_article_tag where article_id=?";
        List<Integer> tagIds = jdbcTemplate.queryForList(sql_article_tag_id, new Object[]{articleId}, Integer.class);
        if (null != tagIds) {
            ArticleTag tag;
            for (int i = 0; i < tagIds.size(); i++) {
                tag = articleTagRepository.findById(tagIds.get(i));
                if (null != tag) {
                    tag.setPosts(tag.getPosts() - 1);
                }
                if (tag.getPosts() < 1) {
                    articleTagRepository.delete(tagIds.get(i));
                } else {
                    articleTagRepository.update(tag);
                }
            }
        }

        // 删除关联表
        String sql_article_tag_rela = "delete from blog_article_tag_rela where article_id=?";
        jdbcTemplate.update(sql_article_tag_rela, new Object[]{articleId});
    }
}
